import { createApp } from "vue";
import { createPinia } from "pinia";
import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import App from "./App.vue";
import "./styles/global.scss";
import router from "./router";
import { useUserStore } from "./store/user";

const app = createApp(App);
const pinia = createPinia();

app.use(pinia);
app.use(ElementPlus);

router.beforeEach((to, from, next) => {
  const userStore = useUserStore(pinia);
  if (to.meta?.requiresAuth) {
    if (userStore.isLoggedIn) {
      next();
      return;
    }
    if (to.meta.allowPartial && userStore.partial) {
      next();
      return;
    }
    next({ name: "home", query: { redirect: to.fullPath } });
    return;
  }
  next();
});

app.use(router);

const userStore = useUserStore(pinia);
userStore.bootstrap();

app.mount("#app");
